package services.common;

import java.util.Date;
import java.util.List;

import models.common.bean.WithdrawalRecord;
import models.common.entity.t_user;
import models.common.entity.t_withdrawal_user;
import services.base.BaseService;

import common.constants.Constants;
import common.enums.Client;
import common.utils.Factory;
import common.utils.PageBean;
import common.utils.ResultInfo;

import daos.common.WithdrawalUserDao;

public class WithdrawalUserService extends BaseService<t_withdrawal_user> {

	protected WithdrawalUserDao withdrawalUserDao = Factory.getDao(WithdrawalUserDao.class);
	
	protected UserFundService userFundService = Factory.getService(UserFundService.class);
	protected UserService userService = Factory.getService(UserService.class);
	protected NoticeService noticeService = Factory.getService(NoticeService.class);
	
	protected WithdrawalUserService() {
		super.basedao = this.withdrawalUserDao;
		
	}
	
	/**
	 * 添加提现记录
	 * @param userId 用户Id
	 * @param serviceOrderNo 业务订单号
	 * @param withdrawalAmt 提现金额
	 * @param bankAccount 银行卡号
	 * @param summary 备注
	 * @param client 登录端
	 * @return
	 *
	 * @author Chenzhipeng
	 * @createDate 2016年1月19日
	 *
	 */
	public boolean addUserWithdrawal(long userId, String serviceOrderNo,
			double withdrawalAmt, String bankAccount, String summary,
			Client client) {
		t_withdrawal_user withdrawalUser = new t_withdrawal_user();
		t_user user = userService.findByID(userId);
		withdrawalUser.time = new Date();
		withdrawalUser.order_no = serviceOrderNo;
		withdrawalUser.user_id = user.id;
		withdrawalUser.name = user.company_full_name;
		withdrawalUser.amount = withdrawalAmt;
		withdrawalUser.bank_account = bankAccount;
		withdrawalUser.summary = summary;
		withdrawalUser.user_or_company = user.is_company;
		withdrawalUser.setStatus(t_withdrawal_user.Status.FAILED);  //先失败后成功
		withdrawalUser.setClient(client);
		
		return withdrawalUserDao.save(withdrawalUser);
	}

	/**
	 * 提现成功，更新相关数据
	 * @param serviceOrderNo
	 * @return
	 *
	 * @author Chenzhipeng
	 * @param summary 
	 * @createDate 2016年1月19日
	 *
	 */
	public int updateWithdrawalToSuccess(String serviceOrderNo, String summary) {

		return withdrawalUserDao.updateWithdrawalToSuccess(serviceOrderNo,summary);
	}

	/**
	 * 提现冻结成功，更新相关数据
	 * @param serviceOrderNo
	 * @return
	 *
	 * @author Chenzhipeng
	 * @createDate 2016年1月19日
	 *
	 */
	public int updateWithdrawalToFreeze(String serviceOrderNo) {

		return withdrawalUserDao.updateWithdrawalToFreeze(serviceOrderNo);
	}

	
	/**
	 * 提现审核不通过
	 * @param withdrawalId
	 * @param summary 
	 * @return
	 */
	public int auditWithdrawalFail(long withdrawalId) {

		return withdrawalUserDao.auditWithdrawalFail(withdrawalId);
	}
	
	public int auditWithdrawalFail2(long withdrawalId, String summary) {
		
		return withdrawalUserDao.auditWithdrawalFail2(withdrawalId,summary);
	}
	/**
	 * 提现记录
	 * @param currPage
	 * @param pageSize
	 * @param userId
	 * @return
	 *
	 * @author Chenzhipeng
	 * @createDate 2016年1月16日
	 *
	 */
	public PageBean<t_withdrawal_user> pageOfDealUser(int currPage,
			int pageSize, long userId) {

		return withdrawalUserDao.pageOfWithdrawal(currPage, pageSize, userId);
	}
	
	/**
	 * 提现记录（后台）
	 * 
	 * @param pageSize
	 * @param currPage
	 * @param exports 1:导出  default：不导出
	 * @param name 会员
	 * 
	 * @author Chenzhipeng
	 * @createDate 2015年12月22日
	 */
	public PageBean<t_withdrawal_user> pageOfWithdrawalRecordBack(int orderValue,int showType, int currPage,int pageSize,int exports,String name) {
		
		return withdrawalUserDao.pageOfWithdrawalRecordBack(orderValue,showType, currPage, pageSize, exports, name);
	}

	/**
	 * 审核提现
	 * @param withdrawawlId
	 * @param status
	 * @param summary 
	 * @return
	 */
	public ResultInfo auditWithdraw(long withdrawawlId, int status, String summary) {
		
		ResultInfo result = new ResultInfo();
		
		if(t_withdrawal_user.Status.FAILED.code == status){
			//审核不通过
			result = userFundService.auditWithdrawalFail2(withdrawawlId,summary);
			
		}else if(t_withdrawal_user.Status.SUCCESS.code == status){
			//审核通过
			t_withdrawal_user wu = t_withdrawal_user.findById(withdrawawlId);
			
			//计算手续费
			//double withdrawalFee = (Constants.WITHDRAW_MAXRATE /100)* wu.amount;
			// TODO
			//result = userFundService.doWithdrawal(wu.user_id, wu.amount, withdrawalFee,withdrawalFee, wu.order_no, true, false,summary);
			result = userFundService.doWithdrawal(wu.user_id, wu.amount, wu.order_no, true, false,summary);
			
		}
		
		return result;
	}

	/**
	 * 查询提现详情
	 * @param withdrawId
	 * @return
	 */
	public t_withdrawal_user queryWithdrawDetail(long withdrawId) {
		
		return withdrawalUserDao.queryWithdrawDetail(withdrawId);
	}

	/**
	 *	查询提现成功的总金额
	 * @param name 会员名
	 * @return
	 * @author LiangQiongfeng
	 *@version 2018-1-17 
	 * @param showType 
	 */
	public double findWithdrawalTotalAmt(int showType, String name) {
		
		return withdrawalUserDao.findWithdrawalTotalAmt(showType,name);
	}


	/**
	 * 统计报表-充值提现
	 * @param orderValue
	 * @param showType
	 * @param currPage
	 * @param pageSize
	 * @param exports
	 * @param time
	 * @return
	 * @author liangqiongfeng
	 *@version 2018-4-10 
	 */
	public PageBean<WithdrawalRecord> pageOfCashValue(int orderValue,
			int showType, int currPage, int pageSize, int exports, Date time) {
		return withdrawalUserDao.pageOfCashValue(orderValue, showType, currPage, pageSize, exports, time);
	}

	/**
	 *
	 * @param showType
	 * @param time
	 * @return
	 * @author xiaojun
	 *@version 2018-4-13 
	 */
	public double findWithdrawalTotalAmt(int showType, Date time) {
		// TODO Auto-generated method stub
		return withdrawalUserDao.findWithdrawalTotalAmt(showType,time);
	}

}
